package com.woniuxy.lab.woniu_lab_boss.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;

/**
 * @author ChenWeiJi
 * @date 2022-11-14
 * @since 1.0
 */
public class OSSUtil {
    /**
     * 节点域名
     */
    private static String host="oss-cn-chengdu.aliyuncs.com";
    /**
     * 身份ID
     */
    private static String keyId="LTAI5tA8hYjjwrfXiKt4RHA4";
    /**
     * 身份凭证
     */
    private static String keySecret = "EYQQFUNNL82HaPTCLFupydWqwEGULV";

    /**
     *上传文件
     * @param file 文件
     * @param name
     ** @param bucketName bocketName组名
     * @return String url
     * @author ChenWeiJi
     * @date 2022/11/14 22:05
     * @since 1.0
     */
    public static String uploadFile(InputStream file, String name, String bucketName){
        OSS ossClient = new OSSClientBuilder().build("https://"+host, keyId, keySecret);
        try {
            ossClient.putObject(bucketName,name,file);
        }finally{
            if(ossClient!=null){
                ossClient.shutdown();
            }
        }
        return "https://"+bucketName+"."+host+"/"+name;
    }


    /**
     *删除文件
     * @param name
     ** @param bucketName bocketName组名
     * @return boolean
     * @author ChenWeiJi
     * @date 2022/11/14 22:05
     * @since 1.0
     */
    public boolean deleteFile(String name ,String bucketName){
        OSS ossClient = new OSSClientBuilder().build("https://"+host, keyId, keySecret);
        try {
            ossClient.deleteObject(bucketName,name);
        }finally{
            if(ossClient!=null){
                ossClient.shutdown();
            }
        }
        return true;
    }

}
